package com.fjxd.order.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjxd.feign.clients.*;
import com.fjxd.feign.pojo.FlightNumber;
import com.fjxd.feign.pojo.People;
import com.fjxd.feign.pojo.Ticket;
import com.fjxd.order.mapper.OrderMapper;
import com.fjxd.order.pojo.Order;
import com.fjxd.order.pojo.vo.CreateOrderVo;
import com.fjxd.order.pojo.vo.OrderVo;
import com.fjxd.order.pojo.vo.TicketVo;
import com.fjxd.order.service.OrderService;

import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.text.View;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * @author csw
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    public static final int UNPAID = 1;// 未支付
    public static final int PAID = 2;// 已支付
    public static final int PASTDUE = 3;// 订单超时
    public static final int RETREAT = 4;//退票
    public static final int CHANGE = 5;//改签
    public static final int DEPART = 6;// 已完成
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserClient userClient;
    @Autowired
    TicketStoreClient ticketStoreClient;
    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public List<Order> getOrderById(Integer id) {
        List<Order> orderById = orderMapper.getOrderById(id);
        for(Order order:orderById){
            List<TicketVo> orderManyTicket = getOrderManyTicket(order.getId());
            List<Integer> list=new ArrayList<>();
            order.setTicketVo(orderManyTicket);
            order.setOrderVo(getOrderManyPeople(order.getId()));
            for (TicketVo ticketVo:order.getTicketVo()){
                list.add(ticketVo.getTickets().getFId());
            }
            order.setFlightNumber(flightNumberClient.getManyMsg(list));
        }

        return orderById;
    }


    @Override
    public Integer deUserMoney(Order order, boolean pay) {
        if (pay) {
            userClient.upMoney(order.getUserId(), order.getOrderMoney());
            try {
                order.setOrderStatus(PAID);
                int i = orderMapper.updateById(order);
            } catch (FeignException e) {
                throw new RuntimeException(e.contentUTF8(), e);
            }
        } else {
            order.setOrderStatus(PASTDUE);
            int i = orderMapper.updateById(order);
        }
        return 1;
    }

    @Override
    public Integer updateOrder(Order order) {
        log.info("改签订单开始");
        log.info("修改订单开始");
        int i = orderMapper.updateById(order);
        try {
            log.info("修改订单成功，开始回增机票");
        } catch (FeignException e) {
            //出现异常，抛出异常
            log.error("订单改签失败,因为：{}",e.contentUTF8(),e);
            throw new RuntimeException(e.contentUTF8(),e);
        }
        log.info("改签成功");
        return i;
    }

    @Autowired
    private PlaneTicketClient planeTicketClient;
    @Autowired
    private PeopleClient peopleClient;
    @Autowired
    private FlightNumberClient flightNumberClient;
    @Override
    public List<Order> getOrderByUserId(Integer id) {
        List<Order> orderByUserId = orderMapper.getOrderByUserId(id);

        for(Order order:orderByUserId){
           List<TicketVo> orderManyTicket = getOrderManyTicket(order.getId());
           List<Integer> list=new ArrayList<>();
           order.setTicketVo(orderManyTicket);
           order.setOrderVo(getOrderManyPeople(order.getId()));
           for (TicketVo ticketVo:order.getTicketVo()){
               list.add(ticketVo.getTickets().getFId());
           }
            order.setFlightNumber(flightNumberClient.getManyMsg(list));
        }

        return orderByUserId;
    }

    @Override
    public boolean delOrderById(Integer id) {
        boolean b1 = orderMapper.deleteByTicket(id);
        boolean b = orderMapper.deleteByPeople(id);
        return orderMapper.deleteOrder(id);

    }

    @Override
    public boolean updateOrderStatusById(Integer id,Integer status) {
        return orderMapper.updateOrderStatusById(id,status);
    }

    @Override
    public List<OrderVo> getOrderManyPeople(Integer orderId) {
        List<OrderVo> orderManyPeople = orderMapper.getOrderManyPeople(orderId);

        for(OrderVo orderVo: orderManyPeople)
        {
            People people=peopleClient.getPeople(orderVo.getPeopleId());
            if (people.getPeopleType().equals("0")){
                people.setPeopleType("普通群众");

            }
            if (people.getPeopleType().equals("1"))
            {
                people.setPeopleType("学生");

            }
            else
            {
                people.setPeopleType("退役士兵");
            }
            orderVo.setPeople(people);
        }

        return orderManyPeople;
    }

    @Override
    public List<TicketVo> getOrderManyTicket(Integer orderId) {
        List<TicketVo> ticketVos=orderMapper.getOrderManyTicket(orderId);
        for (TicketVo ticketVo:ticketVos)
        {
            ticketVo.setTickets(planeTicketClient.getTicketId(ticketVo.getTicketId()));
        }
        return ticketVos;
    }

    @Override
    public Integer createOrder(CreateOrderVo createOrderVo) {
        int insert = orderMapper.insert(createOrderVo.getOrder());
        Integer orderId = createOrderVo.getOrder().getId();
        for (Integer pId:createOrderVo.getPeopleId()){
            orderMapper.insertorderVo(pId,orderId);
        }
        System.out.println(createOrderVo.getTicketId());
        for (int i =0;i<createOrderVo.getTicketId().size();i++)
        {

            orderMapper.insertticketVo(orderId,createOrderVo.getTicketId().get(i),createOrderVo.getTicketStoreId().get(i));
            ticketStoreClient.ticketDeduct(createOrderVo.getTicketId().get(i),Integer.parseInt(createOrderVo.getOrder().getTicketType()),1);

        }
        return createOrderVo.getOrder().getId();
    }
    @Autowired
    private TicketStoreClient storeClient;
    @Override
    public boolean retreatTicket(List<Integer> storeId, Integer count) {
        for(Integer i:storeId)
        {
            System.out.println(i);
            storeClient.ticketGoBack(i,1);
        }
        return false;
    }


}